package com.hongkzh.www.shop.service;

import com.hongkzh.www.adv.service.BaseService;
import com.hongkzh.www.conmon.StaticPublic;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;

import java.util.List;

public class ProductService extends BaseService {
    public Page<Record> getCategoryProductList(String categoryId, int pageNumber, String sortId, String sortValue) {

        Kv cond=Kv.by("categoryId",categoryId).set("order", " order by "+ sortId+" "+sortValue);
        SqlPara sqlPara = Db.getSqlPara("shop.getCategoryProductList",cond);
        Page<Record> records=Db.paginate(pageNumber,StaticPublic.PAGESIZE,sqlPara);

        for (Record r:records.getList()) {
            doFilePath(r);

        }
        return records;
    }

    /**
     * 新品列表
     * @param pageNumber
     * @param sortId
     * @param sortValue
     * @return
     */
    public Page<Record> getNewProductList( int pageNumber, String sortId, String sortValue) {
        Kv cond=Kv.by("isNew","1").set("order", " order by "+ sortId+" "+sortValue);
        SqlPara sqlPara = Db.getSqlPara("shop.getNewProductList",cond);
        Page<Record> records=Db.paginate(pageNumber,StaticPublic.PAGESIZE,sqlPara);

        for (Record r:records.getList()) {
            doFilePath(r);

        }
        return records;
    }

    /**
     * 专题活动
     * @return
     */
    public List<Record> getSpecialsList() {
        List<Record> records=Db.find(Db.getSql("shop.getSpecialsList"));
        for (Record r:records
                ) {
            doFilePath(r);
        }
        return records.size()>0?records:null;
    }

    /**
     * 爆款
     * @return
     */
    public List<Record> getLuckyBurstsList() {
        List<Record> records=Db.find(Db.getSql("shop.getLuckyBurstsList"));
        for (Record r:records
                ) {
            doFilePath(r);
        }
        return records.size()>0?records:null;
    }

    /**
     * 秒杀
     * @return
     */
    public List<Record> getSeckillsList() {
        List<Record> records=Db.find(Db.getSql("shop.getSeckillsList"));
        int currentHour,beginDate,endDate;
        for (Record r:records
                ) {
            currentHour=r.getInt("currentHour");
            beginDate=r.getInt("beginDate");
            endDate=r.getInt("endDate");
            r.set("sortDate", currentHour-beginDate);
            if(currentHour>=beginDate&&currentHour<endDate){
                r.set("sortDate",0);
            }
            else{
                if(currentHour-beginDate>0) {
                    r.set("sortDate", currentHour-beginDate-24);
                }
            }
            doFilePath(r);
        }
        return records.size()>0?records:null;
    }

    /**
     * 折扣
     * @return
     */
    public List<Record> getdDiscountsList() {
        List<Record> records=Db.find(Db.getSql("shop.getdDiscountsList"));
        for (Record r:records
                ) {

            doFilePath(r);
        }
        return records.size()>0?records:null;
    }

    /**
     * 分类
     * @return
     */
    public List<Record> getCategorysList() {

        List<Record> categorys=Db.find(Db.getSql("shop.getRecProductCategory"));
        for (Record r:categorys
                ) {
            List<Record> lists=Db.find(Db.getSqlPara("shop.getRecProductCategoryList",r.getStr("categoryId")));
            for (Record l:lists
                    ) {
                doFilePath(l);
            }
            doFilePath(r);
            r.set("products",lists);
        }
        return categorys.size()>0?categorys:null;
    }

    /**
     * 新品推荐
     * @return
     */
    public List<Record> getNewRecommendsList() {
        List<Record> records=Db.find(Db.getSql("shop.getNewRecommendsList"));
        for (Record r:records
                ) {
            doFilePath(r);
        }
        return records.size()>0?records:null;

    }

    /**
     * 折扣分页
     * @param pageNumber
     * @return
     */
    public Page<Record> getdDiscountsList(int pageNumber) {
           SqlPara sqlPara=Db.getSqlPara("shop.getdDiscountsALLList");
        Page<Record> records=Db.paginate(pageNumber,StaticPublic.PAGESIZE,sqlPara);

        for (Record r:records.getList()
                ) {

            doFilePath(r);
        }
        return records;
    }

    /**
     * 爆款列表
     * @param pageNumber
     * @return
     */
    public Page<Record> getLuckyBurstALLList(int pageNumber) {
        SqlPara sqlPara=Db.getSqlPara("shop.getLuckyBurstALLList");
        Page<Record> records=Db.paginate(pageNumber,StaticPublic.PAGESIZE,sqlPara);
        List<Record> users=null;
        for (Record r:records.getList()
                ) {
            users=Db.find(Db.getSqlPara("shop.getUsersByProductId",r.getStr("productId")));
            for (Record u:users
                 ) {
                doFilePath(u);
            }
            doFilePath(r);
            r.set("users",users);

        }
        return records;
    }

    /**
     * 秒杀
     * @param pageNumber
     * @return
     */
    public Page<Record> getSeckillALLList(int pageNumber) {
        SqlPara sqlPara=Db.getSqlPara("shop.getSeckillALLList");
        Page<Record> records=Db.paginate(pageNumber,StaticPublic.PAGESIZE,sqlPara);
        List<Record> users=null;
        int currentHour=0,beginDate=0,endDate=0 ;
        String currentTime="";
        for (Record r:records.getList()
                ) {
            users=Db.find(Db.getSqlPara("shop.getUsersByProductId",r.getStr("productId")));
            for (Record u:users
                    ) {
                doFilePath(u);
            }
            doFilePath(r);
            r.set("users",users);


            if(null!=records&&null!=records.getList()&&records.getList().size()>0) {
                currentHour = r.getInt("currentHour");
                beginDate =  r.getInt("beginDate");
                endDate = r.getInt("endDate");
                currentTime= r.getStr("currentTime");
            }
            r.set("currentTime", currentTime);
            r.set("beginDate", beginDate);
            r.set("endDate", endDate);
            r.set("currentHour", currentHour);
            r.set("sortDate", currentHour-beginDate);
            if(currentHour>=beginDate&&currentHour<endDate){
                r.set("sortDate",0);
            }
            else{
                if(currentHour-beginDate>0) {
                    r.set("sortDate", currentHour-beginDate-24);
                }
            }
        }
        return records;

    }

    public Record getProductSkuBySku(String skuId) {
        SqlPara sqlPara=Db.getSqlPara("shop.getProductSkuBySku",skuId);
        return  Db.findFirst(sqlPara);

    }

    public List<Record> getProductSkuImagesBySku(String skuId) {
        SqlPara sqlPara=Db.getSqlPara("shop.getProductSkuImagesBySku",skuId);
        List<Record> list=Db.find(sqlPara);

        return list.size()>0?list:null;
    }

    public Page<Record> getProductCommentById(String productId, int pageNumber) {
        SqlPara sqlPara=Db.getSqlPara("shop.getProductCommentById",productId);
        Page<Record> records=Db.paginate(pageNumber,StaticPublic.PAGESIZE,sqlPara);
        List<Record> images=null;
        for (Record r:records.getList()
                ) {
            r.set("images",null);
            images=Db.find(Db.getSqlPara("shop.getProductCommentImagesByCommentId",r.getStr("commentId")));
            for (Record u:images
                    ) {
                doFilePath(u);
            }
            doFilePath(r);
            if(null!=images&&images.size()>0)
            {r.set("images",images);}

        }
        return records;
    }

    public Record getProductById(String productId) {
        SqlPara sqlPara=Db.getSqlPara("shop.getProductById",productId);
        Record record=Db.findFirst(sqlPara);
        sqlPara=Db.getSqlPara("shop.getProductImagesById",productId);
        List<Record>images=Db.find(sqlPara);
        for (Record u:images
                ) {
            doFilePath(u);
        }

        record.set("images",images);



        sqlPara=Db.getSqlPara("shop.getProductColorsById",productId);
        List<Record>colors=Db.find(sqlPara);
        record.set("colors",colors);


        sqlPara=Db.getSqlPara("shop.getProductSpecsById",productId);
        List<Record>specs=Db.find(sqlPara);
        record.set("specs",specs);


        sqlPara=Db.getSqlPara("shop.getProductSkusById",productId);
        List<Record>skus=Db.find(sqlPara);
        record.set("skus",skus);

        sqlPara=Db.getSqlPara("shop.getProductCommentById",productId);
        Record comment=Db.findFirst(sqlPara);

       if(null!=comment){
            images=Db.find(Db.getSqlPara("shop.getProductCommentImagesByCommentId",comment.getStr("commentId")));
            for (Record u:images
                    ) {
                doFilePath(u);
            }
            doFilePath(comment);
           comment.set("images",null);
            if(null!=images&&images.size()>0){
                comment.set("images",images);
            }

        }
        record.set("comment",comment);



        sqlPara=Db.getSqlPara("shop.getProductRecsById",productId);
        List<Record>recs=Db.find(sqlPara);
        for (Record u:recs
                ) {
            doFilePath(u);
        }
        record.set("recs",recs);


        return record;
    }
}
